package com.thinkgem.jeesite.common.utils;

/**
 * @author zhangchong zhangc@passiontec.cn
 * @since 2018/1/18 23:45
 */
public class ColorUtils{

    public static String rgba2rgb(String rgba){
        if(rgba.length() <= 10){
            return rgba.replace("#", "");
        }
        else{
            rgba = rgba.replace("rgba(", "").replace(")", "");
            String[] rgbas = rgba.split(",");
            String r = rgbas[0].trim();
            String g = rgbas[1].trim();
            String b = rgbas[2].trim();

            r = Integer.toHexString(Integer.parseInt(r));
            g = Integer.toHexString(Integer.parseInt(g));
            b = Integer.toHexString(Integer.parseInt(b));
            String a = "";
            if(rgbas.length >= 4){
                a = rgbas[3].trim();
                a = Integer.toHexString((int)(Double.parseDouble(a) * 255));
            }
            return a + r + g + b;
        }
    }

    public static String eight2rgba(String eight){
        if(eight.length() < 8){
            return eight;
        }
        if(eight.contains("rgba")){
            return eight;
        }
        if(StringUtils.isNotEmpty(eight)){
            eight = eight.replace("#", "");
            int r = Integer.parseInt(eight.substring(2, 4), 16);
            int g = Integer.parseInt(eight.substring(4, 6), 16);
            int b = Integer.parseInt(eight.substring(6, 8), 16);
            double a = ((double)Integer.parseInt(eight.substring(0, 2), 16)) / 255;
            return "rgba(" + r + "," + g + "," + b + "," + a + ")";
        }
        return "";
    }
    public static void main(String[] args){
        String rgba = "rgba(187, 255, 255, 0.25)";
        String rgba1 = "#222222";
        System.out.println(rgba2rgb(rgba));
        System.out.println(rgba2rgb(rgba1));
    }
}
